using System;
using System.Collections.Generic;
using BBX.Common;
using BBX.Entity;
using BBX.Forum;

namespace BBX.Web
{
    public class search : PageBase
    {
        public Int32 searchid = DNTRequest.GetInt("searchid", -1);
        public Int32 pageid = DNTRequest.GetInt("page", 1);
        public Int32 topiccount;
        public Int32 pagecount;
        public String pagenumbers = "";
        public Int32 searchresultcount;
        public IList<Topic> topiclist = new List<Topic>();
        public Boolean searchpost;
        public String type = DNTRequest.GetHtmlEncodeString("type", true).ToLower();
        public Int32 topicpageid = DNTRequest.GetInt("topicpage", 1);
        public Int32 topicpagecount;
        public String topicpagenumbers = "";
        private String msg = "";
        public String keyword = DNTRequest.GetHtmlEncodeString("keyword").Trim();
        public String poster = DNTRequest.GetHtmlEncodeString("poster").Trim();
        public Int32 advsearch = DNTRequest.GetInt("advsearch", 0);
        public Int32 searchsubmit = DNTRequest.GetInt("searchsubmit", 0);
        public SearchType searchType;

        protected override void ShowPage()
        {
            pagetitle = "搜索";
            GetSearchType();
            if (searchsubmit == 0 && !ispost)
            {
                if (!UserAuthority.Search(usergroupinfo, ref msg))
                {
                    base.AddErrLine(msg);
                    return;
                }
                if (searchid <= 0) return;

                if (searchType == SearchType.Error)
                {
                    base.AddErrLine("非法的参数信息");
                    return;
                }
                switch (searchType)
                {
                    case SearchType.ByPoster:
                        topiclist = SearchCache.GetSearchCacheList(searchid, 16, topicpageid, out topiccount, SearchType.TopicTitle);
                        topicpageid = CalculateCurrentPage(topiccount, topicpageid, out topicpagecount);
                        topicpagenumbers = ((topicpagecount > 1) ? Utils.GetPageNumbers(topicpageid, topicpagecount, "search.aspx?type=" + type + "&searchid=" + searchid + "&keyword=" + keyword + "&poster=" + poster, 8, "topicpage", "#1") : "");
                        return;
                }
                topiclist = SearchCache.GetSearchCacheList(searchid, 16, pageid, out topiccount, searchType);
                if (topiccount == 0)
                {
                    base.AddErrLine("不存在的searchid");
                    return;
                }
                CalculateCurrentPage();
                pagenumbers = ((pagecount > 1) ? Utils.GetPageNumbers(pageid, pagecount, "search.aspx?type=" + type + "&searchid=" + searchid + "&keyword=" + keyword + "&poster=" + poster, 8) : "");
                return;
            }
            else
            {
                if (!UserAuthority.Search(userid, lastsearchtime, useradminid, usergroupinfo, ref msg))
                {
                    base.AddErrLine(msg);
                    return;
                }
                if (searchType == SearchType.Error)
                {
                    base.AddErrLine("非法的参数信息");
                    return;
                }
                searchpost = true;
                var searchforumid = DNTRequest.GetString("searchforumid").Trim();
                var posterid = CheckSearchInfo(searchforumid);
                if (base.IsErr()) return;

                searchid = SearchCache.Search(userid, usergroupid, keyword, posterid, searchType, searchforumid, DNTRequest.GetInt("searchtime", 0), DNTRequest.GetInt("searchtimetype", 0), DNTRequest.GetInt("resultorder", 0), DNTRequest.GetInt("resultordertype", 0));
                if (searchid > 0)
                {
                    Response.Redirect(forumpath + "search.aspx?type=" + type + "&searchid=" + searchid + "&keyword=" + keyword + "&poster=" + poster, false);
                    return;
                }
                base.AddErrLine("抱歉, 没有搜索到符合要求的记录");
                return;
            }
        }

        private void CalculateCurrentPage()
        {
            pagecount = ((topiccount % 16 == 0) ? (topiccount / 16) : (topiccount / 16 + 1));
            pagecount = ((pagecount == 0) ? 1 : pagecount);
            pageid = ((pageid < 1) ? 1 : pageid);
            pageid = ((pageid > pagecount) ? pagecount : pageid);
        }

        private Int32 CalculateCurrentPage(Int32 listcount, Int32 pageid, out Int32 pagecount)
        {
            pagecount = ((listcount % 16 == 0) ? (listcount / 16) : (listcount / 16 + 1));
            pagecount = ((pagecount == 0) ? 1 : pagecount);
            pageid = ((pageid < 1) ? 1 : pageid);
            pageid = ((pageid > pagecount) ? pagecount : pageid);
            return pageid;
        }

        private void GetSearchType()
        {
            String key;
            switch (key = type)
            {
                case "":
                case "topic":
                    searchType = SearchType.TopicTitle;
                    return;
                case "author":
                    searchType = SearchType.ByPoster;
                    return;
                case "post":
                    searchType = SearchType.PostContent;
                    return;
                case "digest":
                    searchType = SearchType.DigestTopic;
                    return;
            }
            searchType = SearchType.Error;
        }

        public String LightKeyWord(String str, String keyword)
        {
            if (string.IsNullOrEmpty(keyword)) return str;

            return str.Replace(keyword, "<font color=\"#ff0000\">" + keyword + "</font>");
        }

        private Int32 CheckSearchInfo(String searchforumid)
        {
            var num = (DNTRequest.GetString("posterid").ToLower().Trim() == "current") ? userid : DNTRequest.GetInt("posterid", -1);
            if (keyword.IsNullOrEmpty() && poster.IsNullOrEmpty() && Utils.StrIsNullOrEmpty(DNTRequest.GetString("posterid")))
            {
                base.AddErrLine("关键字和用户名不能同时为空");
                return num;
            }
            if (num > 0 && BBX.Entity.User.FindByID(num) == null)
            {
                base.AddErrLine("指定的用户ID不存在");
                return num;
            }
            if (!poster.IsNullOrEmpty())
            {
                num = Users.GetUserId(poster);
                if (num == -1)
                {
                    base.AddErrLine("搜索用户名不存在");
                    return num;
                }
            }
            if (!searchforumid.IsNullOrEmpty())
            {
                var array = Utils.SplitString(searchforumid, ",");
                for (var i = 0; i < array.Length; i++)
                {
                    var expression = array[i];
                    if (!Utils.IsNumeric(expression))
                    {
                        base.AddErrLine("非法的搜索版块ID");
                        return num;
                    }
                }
            }
            return num;
        }
    }
}